package com.example.myshop.domain;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.myshop.entity.Cart;
import com.example.myshop.vo.CartVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CartDao extends BaseMapper<Cart> {
    /**
     * 根据用户uid查询VO类用户加入购物车的商品列表
     * @param uid
     * @return
     */
    @Select("SELECT cid, uid, pid,\n" +
            "t_cart.price, t_cart.num,\n" +
            "t_product.title, t_product.price AS realPrice, t_product.image\n" +
            "FROM t_cart LEFT JOIN t_product ON t_cart.pid = t_product.id\n" +
            "WHERE uid = #{uid}\n" +
            "ORDER BY t_cart.created_time DESC")
    List<CartVO> findVOByUid(Long uid);

    /**
     * 根据若干个购物车数据id查询详情的列表
     * @param cid
     * @return
     */
    @Select("<script>SELECT cid, uid, pid, t_cart.price, t_cart.num,\n" +
            "t_product.title, t_product.price AS realPrice, t_product.image\n" +
            "FROM t_cart\n" +
            "LEFT JOIN t_product ON t_cart.pid = t_product.id\n" +
            "WHERE\n" +
            "cid IN (\n" +
            "<foreach collection=\"array\" item=\"cid\" separator=\",\">\n" +
            "#{cid}\n" +
            "</foreach>\n" +
            ")\n" +
            "ORDER BY\n" +
            "t_cart.created_time DESC</script>")
    List<CartVO> findVOByCids(Long[] cid);

}
